using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;

using Entities;
using IDAL;

namespace SQLDBDAL
{
    public class FactoryDAO : IFactoryDAO
    {
        //query strings
        private const string SQL_SELECT_FACTORY = "SELECT * FROM Factory";
        private const string SQL_INSERT_FACTORY = "INSERT INTO Factory (CompanyID, FactoryName, Description) VALUES (@CompanyID, @FactoryName, @Description)";
        private const string SQL_UPDATE_FACTORY = "UPDATE Factory SET CompanyID=@CompanyID, FactoryName=@FactoryName, Description=@Description WHERE FactoryID=@FactoryID";

        //parameters
        private const string PARAM_COMPANY_ID = "@CompanyID";
        private const string PARAM_FACTORY_NAME = "@FactoryName";
        private const string PARAM_DESCRIPTION = "@Description";
        private const string PARAM_FACTORY_ID = "@FactoryID";

        #region IFactoryDAO Members

        public IList<FactoryInfo> GetAll()
        {
            IList<FactoryInfo> factories = new List<FactoryInfo>();
            try
            {
                SqlDataReader reader = SQLDBHelper.ExecuteReader(SQLDBHelper.CONN_STRING, CommandType.Text, SQL_SELECT_FACTORY, null);
                while (reader.Read())
                {
                    FactoryInfo fac = new FactoryInfo();
                    fac.FactoryID = reader.GetInt32(0);
                    fac.CompanyID = reader.GetInt32(1);
                    fac.FactoryName = reader.GetString(2);
                    fac.Description = reader.GetString(3);
                    factories.Add(fac);
                }
                return factories;
            }
            catch (Exception e)
            {
                throw e;
            }
        }

        public bool Insert(FactoryInfo fac)
        {
            SqlParameter[] parms = new SqlParameter[] {
                                                            new SqlParameter(PARAM_COMPANY_ID, SqlDbType.Int),
                                                            new SqlParameter(PARAM_FACTORY_NAME, SqlDbType.NVarChar),
                                                            new SqlParameter(PARAM_DESCRIPTION, SqlDbType.NVarChar), 
                                                            new SqlParameter(PARAM_FACTORY_ID, SqlDbType.Int) };
            parms[0].Value = fac.CompanyID;
            parms[1].Value = fac.FactoryName;
            parms[2].Value = fac.Description;
            parms[3].Value = fac.FactoryID;

            try
            {
                int result = 0;
                SqlConnection conn = new SqlConnection(SQLDBHelper.CONN_STRING);
                result = SQLDBHelper.ExecuteNonQuery(conn, CommandType.Text, SQL_INSERT_FACTORY, parms);
                SQLDBHelper.SetNewIdentity(conn, fac.FactoryID);
                return (result > 0 ? true : false);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public bool Update(FactoryInfo fac)
        {
            SqlParameter[] parms = new SqlParameter[] {
                                                            new SqlParameter(PARAM_COMPANY_ID, SqlDbType.Int),
                                                            new SqlParameter(PARAM_FACTORY_NAME, SqlDbType.NVarChar),
                                                            new SqlParameter(PARAM_DESCRIPTION, SqlDbType.NVarChar),
                                                            new SqlParameter(PARAM_FACTORY_ID, SqlDbType.Int) };
            parms[0].Value = fac.CompanyID;
            parms[1].Value = fac.FactoryName;
            parms[2].Value = fac.Description;
            parms[3].Value = fac.FactoryID;

            try
            {
                int result = SQLDBHelper.ExecuteNonQuery(SQLDBHelper.CONN_STRING, CommandType.Text, SQL_UPDATE_FACTORY, parms);
                return (result > 0 ? true : false);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        #endregion
    }
}
